package com.example.myschool.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.myschool.ProductDetailsActivity;
import com.example.myschool.R;
import com.example.myschool.adapter.LeftListAdapter;
import com.example.myschool.adapter.RightListAdapter;
import com.example.myschool.entity.DataService;
import com.example.myschool.entity.ProductInfo;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
   private View rootview;
   private RecyclerView leftRecyclerView;
   private RecyclerView rightRecyclerView;
   private LeftListAdapter mLeftListAdapter;
   private RightListAdapter mRightListAdapter;
   private List<String> leftDataList =new ArrayList<>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootview= inflater.inflate(R.layout.fragment_home, container, false);

        leftRecyclerView= rootview.findViewById(R.id.leftRecyclerView);
        rightRecyclerView= rootview.findViewById(R.id.rightRecyclerView);

        return rootview;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        leftDataList.add("食品");
        leftDataList.add("饮料");
        leftDataList.add("服装");
        leftDataList.add("二手书");
        leftDataList.add("日用品");
        leftDataList.add("化妆品");



        mLeftListAdapter=new LeftListAdapter(leftDataList);
        leftRecyclerView.setAdapter(mLeftListAdapter);

        mRightListAdapter=new RightListAdapter();
        rightRecyclerView.setAdapter(mRightListAdapter);

        mRightListAdapter.serListData(DataService.getListData(0));

        mRightListAdapter.setmOnItemClickListener(new RightListAdapter.onItemClickListener() {
            @Override
            public void onItemClick(ProductInfo productInfo, int position) {
                Intent intent=new Intent(getActivity(), ProductDetailsActivity.class);
                intent.putExtra("productInfo",productInfo);
                startActivity(intent);
            }
        });

        mLeftListAdapter.setmLeftListOnClickItemListener(new LeftListAdapter.LeftListOnClickItemListener() {
            @Override
            public void onClickItemListener(int position) {
                mLeftListAdapter.setCurrentIndex(position);
                mRightListAdapter.serListData(DataService.getListData(position));
            }
        });
    }
}